var zTreeUtil = {
	setting: {
		view: { dblClickExpand: false, selectedMulti: false },
		data: { simpleData: { enable: true } },
		callback: { 
			onClick: function(e, treeId, treeNode) {
				var entity = treeId.substring('select_tree_'.length);
				var zTree = $.fn.zTree.getZTreeObj("select_tree_" + entity);
				var node = zTree.getSelectedNodes()[0];
				var obj = $("#select_" + entity);
				var pre = zTreeUtil.getPre(obj.attr("name"));
				obj.val(node.name);
				$("input[name='"+ pre + entity +".id']").val(node.id);
				$(".menuContent").fadeOut("fast");
			}
		}
	},
	getPre: function(name) {
		if(!!name) {
			name = name.substring(0, name.lastIndexOf("_") + 1);
		}
		return name || "";
	},
	showMenu: function(entity) {
		var cityObj = $("#select_" + entity);
		var cityOffset = $("#select_" + entity).offset();
		$("#select_tree_menu_" + entity).css({left:cityOffset.left + "px", top:cityOffset.top + cityObj.outerHeight() + "px"}).slideDown("fast");
		$("body").bind("mousedown", this.onBodyDown);
		//var entityId = $("input[name='"+ entity +".id']").val();
		var entityId = cityObj.siblings("input[name='"+ entity +".id']").val();
		if(entityId) {
			var zTree = $.fn.zTree.getZTreeObj("select_tree_" + entity);
			var node = zTree.getNodeByParam("id", entityId, null);
			node && zTree.selectNode(node);
		}
	},
	onBodyDown: function(event) {
		var that = this;
		if (!($(event.target).parents(".menuContent").length>0)) {
			$(".menuContent").fadeOut("fast");
			$("body").unbind("mousedown", that.onBodyDown);
		}
	},
	/**
	 * 初始化
	 * @param id input输入框的id，select_实体名。例如 select_fiscal
	 * @param nodesData 树的数据，json格式
	 */
	init: function(id, nodesData){
		var that = this;
		var entity = id.substring("select_".length);
		var treeId = "select_tree_" + entity;
		$.fn.zTree.init($("#" + treeId), that.setting, nodesData);
		$("#" + id).click(function(){
			that.showMenu(entity);
		});
	}
}